<div class="page-body">
    <div class="row">
        <div class="widget">
            <div class="widget-header
            <switch name="T_type">
            <case value="1">bg-magenta</case>
            <case value="2">bg-palegreen</case>
            <case value="3">bg-lightred</case>
            <case value="4">bg-blue</case>
            <case value="5">bg-blueberry</case>
            <case value="6">bg-gold</case>
            <default />bg-magenta
            </switch>
                                ">
            <span class="widget-caption">{$T_title|default=CONTROLLER_NAME}</span>
                                <!--Widget Buttons-->
        </div>
            <div class="widget-body">
                <div id="horizontal-form">
                    <form class="form-horizontal form_buy" role="form"
                          action="{:U('doReturn')}"
                          method="post" enctype="multipart/form-data" onSubmit="return false;">
                        <div class="form-group">
                            <label for="card_id" class="col-sm-3 control-label no-padding-right">会员卡：</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" id="card_id" name="card_id" value="" onkeydown="getCard()">
                                <div id="card_info" style="display:none;">
                                </div>
                            </div>
                        </div>
                        <hr>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">
                                条形码：</label>
                            <div class="col-sm-3">
                                <input type="text" id="tm" name="tm" class="form-control" value="" onkeydown="addBook()">
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right"></label>
                            <div class="col-sm-6">
                                <!--测试用：23132132132&nbsp;&nbsp;123625136251&nbsp;&nbsp;111111111111-->
                                <table class="table table-hover" aria-describedby="simpledatatable_info">
                                    <thead class="bordered-darkorange">
                                    <tr>
                                        <th>ID</th>
                                        <th>条码</th>
                                        <th>书名</th>
                                        <th>借阅</th>
                                        <th>归还/破损</th>
                                        <th>定价/处理价</th>
                                        <th>状态</th>
                                        <th>借阅时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="order_book_list">
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <hr>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">借书明细：</label>
                            <div class="col-sm-6">
                                <table class="table table-hover" aria-describedby="simpledatatable_info">
                                    <thead class="bordered-darkorange">
                                    <tr>
                                        <th>ID</th>
                                        <th>条码</th>
                                        <th>书名</th>
                                        <th>借阅</th>
                                        <th>归还/破损</th>
                                        <th>借阅时间</th>
                                        <th>状态</th>
                                    </tr>
                                    </thead>
                                    <tbody id="record_list">

                                    </tbody>
                                    <tfoot id="record_page" style="display:none;">
                                        <tr>
                                            <td colspan="3" class="page_info">
                                                共<span class="num">3</span>条记录,当前第<span class="page">1</span>页
                                            </td>
                                            <td colspan="3">
                                                <!--查看第-->
                                                <!--<select id="page_select" onchange="getRecord()" >-->
                                                    <!--<option value="1">1</option>-->
                                                <!--</select>页-->
                                            </td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                        <div class="form-group  ">
                            <div class="col-sm-offset-3 col-sm-10">
                                <p class="label label-darkpink graded error_info"></p><br>
                                <!--<input type="button" class="btn btn-primary" value="提交" onkeypress="FSubmit()" onclick="FSubmit()">-->
                                <input type="hidden" id="user_id" name="user_id" value="">
                                <!--图书列表-->
                                <input type="hidden" id="liststr" name="liststr" value="">
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function cal_all(){

    }
    //获取会员卡成功后回调
    function getsuccess(data){
        $("#user_id").val(data.data.user_id);
        $("#discount_buy").val(data.data.discount_buy);

        $("#card_info").show();
        getRecord();
        $("#tm").focus();
    }
    //获取当前会员卡已借阅记录
    function getRecord() {
        var $user_id = $("#user_id").val();
        var $list = $("#record_list");
        var $page=$("#page_select").val();
        $list.html("");
        $("#record_page").hide();
        $.post("{:U('Rent/getRecord')}", {"user_id": $user_id,"page":$page}, function (data) {
            if (data.status == "success") {
                //添加借阅记录
                $.each(data.data, function (i, item) {
                    if(item.is_backed=="1"){
                        item.is_backed="已归还";
                        var $class="";
                    }else{
                        item.is_backed="未归还";
                        var $class="warning";
                    }
                    var $str = "<tr class='"+$class+"'>" +
                            "<td>" + item.order_id + "</td>" +
                            "<td>" +
//                            "" +item.book_id+"/" +
                            ""+ item.tm + "</td>" +
                            "<td>《" + item.sm + "》</td>" +
                            "<td>" + item.quantity + "本</td>" +
                            "<td>" + item.num_backed + "本/" + item.num_broken + "本</td>" +
                            "<td>" + item.create_time + "</td>" +
                            "<td>" + item.is_backed + "</td>" +
                            "</tr>";
                    $list.append($str);
                });
                $("#page_select").html("");
                if(data.total=="0"){
                    data.total=1;
                }
                for(var i=1;i<=data.total;i++){
                    if(i==data.page){
                        $str='<option value="'+i+'" selected="selected">'+i+'</option>';
                    }else{
                        $str='<option value="'+i+'">'+i+'</option>';
                    }
                    $("#page_select").append($str);
                }
                $("#record_page .page_info .num").html(data.total);
                $("#record_page .page_info .page").html($("#page_select").val());
                $("#record_page").show();
            } else {
                console.debug(data.data);
            }
            $("#page_select").focus();
        }, "json");
    }

    //添加图书
    function addBook() {

        $("#tm").focus();
        var keyCode = null;

        if (event.which)
            keyCode = event.which;
        else if (event.keyCode)
            keyCode = event.keyCode;

        if (keyCode == 13) {

            var $tm = $("#tm").val();
            if ($.trim($tm) == "") {
                $("#tm").val("");
                $("#tm").focus();
                return;
            }
            if ($("#user_id").val() == "") {
                $(".error_info").html("请输入会员卡");
                return;
            }else{
                $user_id=$("#user_id").val();
                $(".error_info").html("");
            }
            $.post("{:U('Return/getOrderBook')}", {"tm": $tm,"user_id":$user_id}, function (data) {
                if (data.status == "error") {
                    $(".order_book_list").html("");
                    $(".error_info").html(data.data);
                } else {
                    //调用模态框选择数据
                    getSelectBook(data);
                }
            }, "json");
        }
    }
    //模态框选择后回调
    function doadd(item){
        $(".order_book_list").html("");
        if(item.is_backed=="1"){
            item.is_backed="已归还";
        }else{
            item.is_backed="未归还";
        }

            $str = '<tr class="active">' +
    //                    '<img src="' + item.b_logo + '" width="100%" />' +
                    '<td><span class="order_id">' + item.order_id + '</span></td>' +
                    '<td>' +
                    '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
//                '/' +
                    '<span class="tm">'+item.tm+'</span></td>' +
                    '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
                    '<td><span class="quantity">' + item.quantity + '</span>本' + '</td>' +
                    '<td><span class="num_backed">' + item.num_backed + '</span>本/<br><span class="num_broken">' + item.num_backed + '</span>本</td>' +
                    '<td>￥<s class="dj">' + item.dj + '</s>元&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
                    '<td>' + item.is_backed + '</td>' +
                    '<td>' + item.create_time + '</td>' +
                    '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="returnBook($(this))">归还</span></td>' +
                    '</tr>';
            $(".order_book_list").prepend($str);
        $(".order_book_list tr .action-btn").click();
        $("#tm").val("");
        $("#tm").focus();
    }
    function returnBook($this) {
        if (!confirm('确认归还?')) {
            $this.parent().parent().remove();
            return;
        }
        $this=$this.parent();
        var $user_id = $("#user_id").val();
        var $order_id=$this.parent().find(".order_id").html();
        var $book_id=$this.parent().find(".book_id").html();
        console.debug($user_id+$order_id+$book_id);
        $.post("{:U('doReturn')}",{"user_id":$user_id,"order_id":$order_id,"book_id":$book_id},function(data){
            if(data.status=="success"){
//                alert(data.data);
                $this.parent().remove();
                //刷新会员借阅记录
                getRecord();
            }else{
                alert(data.data);
                return;
            }

        },"json");
    }

</script>